package com.dhwl.hsh.merchant.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.dhwl.hsh.lib.bean.resp.BannerResponse;
import com.dhwl.hsh.lib.core.HshException;
import com.dhwl.hsh.lib.core.mvp.MvpView;
import com.dhwl.hsh.lib.utils.SystemTool;
import com.dhwl.hsh.lib.view.viewpagerindicator.IconPageIndicator;
import com.dhwl.hsh.merchant.R;
import com.dhwl.hsh.merchant.presenter.HomePresenter;
import com.dhwl.hsh.merchant.ui.activity.AddTaskActivity;
import com.dhwl.hsh.merchant.ui.activity.RepaymentActivity;
import com.dhwl.hsh.merchant.ui.adapter.BannerViewPagerAdapter;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * 首页
 *
 * @author Derek.Kidd
 * @time 2016/4/20 9:44
 */
public class HomeFragment extends BaseFragment implements MvpView{

    /**
     * 广告banner
     */
    @Bind(R.id.vp_banner)
    ViewPager viewPage;

    /**
     * 点点点
     */
    @Bind(R.id.indicator)
    IconPageIndicator pageIndicator;
    @Bind(R.id.home_tv_tel)
    TextView tvTel;
    @Bind(R.id.home_tv_debt)
    TextView tvDebt;
    @Bind(R.id.home_tv_start_order)
    TextView tvStartOrder;


    HomePresenter homePresenter;

    List<BannerResponse> bannerResponses;

    boolean isDestroyView = true;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_home;
    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        isDestroyView = false;
        if (null != this.self) {
            handler.sendEmptyMessageDelayed(0, 4000);
        }
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    protected void initViews(View self, Bundle savedInstanceState) {
        setTitleContent(R.string.function_bar_home);
    }

    @Override
    protected void initData() {

        homePresenter = new HomePresenter();
        homePresenter.attachView(this);
        homePresenter.getBanner();

        bannerResponses = new ArrayList<>();
    }

    @OnClick(R.id.home_tv_tel)
    public void onTelClick(View  view){
        SystemTool.call(getActivity(),getString(R.string.tel_400));
    }
    @OnClick(R.id.home_tv_debt)
    public void onDebtClick(View  view){
        Intent intent = new Intent();
        intent.setClass(getActivity(), RepaymentActivity.class);
        startActivity(intent);
    }
    @OnClick({R.id.layout_start_task,R.id.home_tv_start_order})
    public void onStartOrderClick(View  view){
        Intent intent = new Intent(getActivity(), AddTaskActivity.class);
        startActivity(intent);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

    //广告栏轮播
    private Handler handler = new Handler() {
        public void handleMessage(android.os.Message msg) {

            if (null != viewPage && bannerResponses.size() > 1) {
                int currentItem = viewPage.getCurrentItem();
                int toItem = (currentItem + 1) == bannerResponses.size() ? 0 : currentItem + 1;
                viewPage.setCurrentItem(toItem, true);
                if (!isDestroyView) {
                    //每4秒钟发送一个message，用于切换viewPager中的图片
                    this.sendEmptyMessageDelayed(0, 4000);
                }
            }
        }
    };

    /**
     * 处理广告banners图片
     *
     * @see [类、类#方法、类#成员]
     */
    private void processFastSelling( ) {

        List<Fragment> picFragList = new ArrayList<Fragment>();
        for (int i = 0; i < bannerResponses.size(); i++) {
            picFragList.add(BannerFragment.newInstants(getActivity(), bannerResponses.get(i)));
        }

        viewPage.setAdapter(new BannerViewPagerAdapter(this.getChildFragmentManager(), picFragList));
        viewPage.setCurrentItem(0);
        pageIndicator.setViewPager(viewPage);
        handler.sendEmptyMessageDelayed(0, 4000);
    }

    @Override
    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart(getActivity().getClass().getSimpleName());
    }

    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd(getActivity().getClass().getSimpleName());
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        isDestroyView = true;
    }

    @Override
    public void onFailure(HshException e) {

    }

    @Override
    public void onSuccess(Object object) {
        bannerResponses = (List<BannerResponse>)object;
        processFastSelling();
    }
}
